home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / rendering / grid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  2.9 KB  |  132 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*  grid.c  - open a window, clear the background, and render a
  19.  *       grid of points or lines centered around the origin.
  20.  *
  21.  *    SPACE key    - toggle between point/line grid
  22.  *    ESCAPE key    - exit program
  23.  */
  24.  
  25. #include <GL/gl.h>
  26. #include <GL/glut.h>
  27.  
  28. #include <stdio.h>
  29.  
  30. /*  Function Prototypes  */
  31.  
  32. GLvoid  initgfx( GLvoid );
  33. GLvoid    keyboard( GLubyte, GLint, GLint );
  34. GLvoid  drawScene( GLvoid );
  35.  
  36. void printHelp( char * );
  37.  
  38. /* Global Definitions */
  39.  
  40. #define KEY_ESC    27    /* ascii value for the escape key */
  41.  
  42. static GLint pointGrid = GL_TRUE;
  43.  
  44. void
  45. main( int argc, char *argv[] )
  46. {
  47.     GLsizei width, height;
  48.  
  49.     glutInit( &argc, argv );
  50.  
  51.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  52.     height = glutGet( GLUT_SCREEN_HEIGHT );
  53.     glutInitWindowPosition( width / 4, height / 4 );
  54.     glutInitWindowSize( width / 2, height / 2 );
  55.     glutInitDisplayMode( GLUT_RGBA );
  56.     glutCreateWindow( argv[0] );
  57.  
  58.     initgfx();
  59.  
  60.     glutKeyboardFunc( keyboard );
  61.     glutDisplayFunc( drawScene ); 
  62.  
  63.     printHelp( argv[0] );
  64.  
  65.     glutMainLoop();
  66. }
  67.  
  68. void
  69. printHelp( char *progname )
  70. {
  71.     fprintf(stdout, "\n%s - demonstrates simple rendering primitives\n\n"
  72.         "SPACE Key        - toggle between point/line grid\n"
  73.         "Escape Key        - exit the program\n\n",
  74.         progname);
  75. }
  76.  
  77. GLvoid
  78. initgfx( GLvoid )
  79. {
  80.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  81.  
  82.     glOrtho( -2.0, 2.0, -2.0, 2.0, -1.0, 1.0 );
  83. }
  84.  
  85. GLvoid 
  86. keyboard( GLubyte key, GLint x, GLint y )
  87. {
  88.     switch (key) {
  89.     case ' ':
  90.         pointGrid = !pointGrid;
  91.         glutPostRedisplay();
  92.         break;
  93.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  94.         exit(0);
  95.     }
  96. }
  97.  
  98. GLvoid
  99. drawScene( GLvoid )
  100. {
  101.     GLfloat x, y;
  102.  
  103.     glClear( GL_COLOR_BUFFER_BIT );
  104.  
  105.     glColor3f( 1.0, 1.0, 1.0 );
  106.  
  107.     if (pointGrid) {
  108.         /* Render a grid composed of evenly spaced points */
  109.         glPointSize( 3.5 ); 
  110.         glBegin( GL_POINTS );
  111.         for ( x = -1.0; x < 1.1; x += 0.1 ) 
  112.             for ( y = -1.0; y < 1.1; y += 0.1 )
  113.                 glVertex2f( x, y );
  114.         glEnd();
  115.     } else {
  116.         /* Draw a line grid centered at the origin */
  117.         glLineWidth( 2.5 );
  118.         glBegin( GL_LINES );
  119.         for ( x = -1.0; x < 1.1; x += 0.1 ) {/* draw vertical lines */
  120.             glVertex2f( x, -1.0 );
  121.             glVertex2f( x, 1.0 );
  122.         }
  123.         for ( y = -1.0; y < 1.1; y += 0.1 ) {/* draw horizontal lines */
  124.             glVertex2f( -1.0, y );
  125.             glVertex2f( 1.0, y );
  126.         }
  127.         glEnd();
  128.     }
  129.  
  130.     glFlush();
  131. }
  132.